package com.gc.services.impl;

import com.gc.common.constants.SysConstants;
import com.gc.dao.custom.UserAssociationMapper;
import com.gc.dao.custom.UserMapper;
import com.gc.dto.UserDTO;
import com.gc.pojo.SysUser;
import com.gc.pojo.SysUserAssociation;
import com.gc.services.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAssociationMapper userAssociationMapper;

    @Override
    public List<UserDTO> getShy() {
        return userMapper.selectUserByRoleId(SysConstants.SYS_ROLE04);
    }


    @Override
    public SysUser getSysUserByUId(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public UserDTO getUserDTOByUId(Integer uid) {
        UserDTO userDTO = new UserDTO();
        SysUser user = userMapper.selectByPrimaryKey(uid);
        SysUserAssociation association = userAssociationMapper.selectByUid(uid);
        userDTO.setLoginName(user.getLoginName());
        userDTO.setPassword(user.getPassword());
        userDTO.setHolder(user.getHolder());
        userDTO.setRoleId(Integer.valueOf(association.getRoles()));
        userDTO.setClientId(association.getClientId());
        userDTO.setTemplateId(association.getTemplateId());
        userDTO.setDeviceId(association.getDeviceId());
        return userDTO;
    }

    @Override
    public int addUser(UserDTO user) {
        SysUser temp = new SysUser();
        temp.setPassword(user.getPassword());
        temp.setHolder(user.getHolder());
        temp.setCreateTime(new Date());
        temp.setLoginName(user.getLoginName());
        temp.setSignature(user.getSignature());
        int count1 = userMapper.insertSelective(temp);
        int count2 = 0;
        SysUserAssociation association = new SysUserAssociation();
        association.setUserId(temp.getUserId());
        association.setRoles(user.getRoleId() + "");
        association.setAuths("1,2,3,4");
        association.setTemplateId(user.getTemplateId());
        association.setDeviceId(user.getDeviceId());
        setMenu(user,association);
        count2 = userAssociationMapper.insertSelective(association);
        return count1 + count2;
    }

    @Override
    public List<UserDTO> getAll() {
        return userMapper.selectAll();
    }

    @Override
    public int remove(Integer userId) {
        int count1 = userAssociationMapper.deleteByUserId(userId);
        int count2 =  userMapper.deleteByPrimaryKey(userId);
        return count1 + count2;
    }

    @Override
    public List<UserDTO> getQfy() {
        return userMapper.selectUserByRoleId(SysConstants.SYS_ROLE03);
    }

    @Override
    public int modifyUser(UserDTO user) {
        SysUser temp = new SysUser();
        temp.setPassword(user.getPassword());
        temp.setHolder(user.getHolder());
        temp.setLoginName(user.getLoginName());
        temp.setUserId(user.getUserId());
        if(!StringUtils.isEmpty(user.getSignature())){
            temp.setSignature(user.getSignature());
        }
        int count1 = userMapper.updateByPrimaryKeySelective(temp);
        SysUserAssociation association = userAssociationMapper.selectByUid(user.getUserId());
        association.setRoles(user.getRoleId()+"");
        association.setUserId(user.getUserId());
        setMenu(user,association);
        //todo 动态设置  不同角色设置的属性值
        if(user.getRoleId() == SysConstants.SYS_ROLE05){
            association.setTemplateId(user.getTemplateId());
            association.setClientId(null);
            association.setDeviceId(user.getDeviceId());
        }else if(user.getRoleId() == SysConstants.SYS_ROLE06){
            association.setTemplateId(null);
            association.setClientId(user.getClientId());
            association.setDeviceId(user.getDeviceId());
        }

       int count2 = userAssociationMapper.updateByPrimaryKey(association);
        return count1 + count2;
    }

    @Override
    public SysUser getUserByLoginName(String loginName) {
        return userMapper.selectByLoginName(loginName);
    }


    private void setMenu(UserDTO user,SysUserAssociation association){
        if(user.getRoleId() == SysConstants.SYS_ROLE06 ||user.getRoleId() == SysConstants.SYS_ROLE05){
            association.setMenusOne("2");
            association.setMenusTwo("3,4,12");
            if(user.getRoleId() == SysConstants.SYS_ROLE06){
                association.setClientId(user.getClientId());
            }
        }else if(user.getRoleId() == SysConstants.SYS_ROLE04){
            association.setMenusOne("2");
            association.setMenusTwo("10,13");
        }else if(user.getRoleId() == SysConstants.SYS_ROLE03){
            association.setMenusOne("2");
            association.setMenusTwo("11,13");
        }else if(user.getRoleId() == SysConstants.SYS_ROLE02){
            association.setMenusOne("3");
            association.setMenusTwo("5,6");
        }else if(user.getRoleId() == SysConstants.SYS_ROLE01){
            association.setMenusOne("1,2,3,4,5");
            association.setMenusTwo("1,2,3,4,5,6,7,8,9,10,11,12,13");
        }
    }
}
